package com.zzx.threekingdom.view;

import com.zzx.threekingdom.R;

import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.ColorFilter;
import android.graphics.Paint;
import android.graphics.RectF;
import android.graphics.drawable.Drawable;
import android.os.Handler;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.Toast;

import java.util.Timer;
import java.util.TimerTask;

public class HintToast
{
    public final static int DEFAULT_DELAYMS = 5000;
    
    private Context mContext;
    private MyBkDrawable myBkDrawable = new MyBkDrawable();
    private TextView mTv;
    private View mContentView;
    private PopupWindow mPopup;
    private Handler handler = new Handler();
    
    public HintToast(Context context)
    {
        mContext = context;
        LayoutInflater mLayoutInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);  
        mContentView = mLayoutInflater.inflate(R.layout.popuptext, null);
        mContentView.setBackgroundDrawable(myBkDrawable);
        mTv = (TextView)mContentView.findViewById(R.id.TextViewHintText);
        mTv.setTextColor(Color.BLACK);
        mPopup = new PopupWindow(mContentView, LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        
    }
    
    public void showHintText(View parent, String hint, int gravity, int xOffset, int yOffset, int delayMillis)
    {
        mTv.setText(hint);
        if(mPopup.isShowing()==false)
            mPopup.showAtLocation(parent, gravity, xOffset, yOffset);
        else
            mPopup.update();
        if(delayMillis>0)
            handler.postDelayed(new Runnable() { public void run() { hideHintText(); } }, delayMillis);

    }
    
    public void showHintText(Activity playActivity, String hint, int yOffset)
    {
        showHintText(playActivity.findViewById(R.id.main), hint, Gravity.BOTTOM, 0, yOffset, 0);
    }
    
    public void hideHintText()
    {
        mPopup.dismiss();
    }
    
    
    private class MyBkDrawable extends Drawable
    {
        @Override
        public void draw(Canvas canvas) {
            Paint paint = new Paint();
            paint.setARGB(205, 255, 255, 255);
            canvas.drawRoundRect(new RectF(0, 0, mContentView.getWidth(), mContentView.getHeight()), 10, 10, paint);
            
        }

        @Override
        public int getOpacity() { return 0; }
        @Override
        public void setAlpha(int alpha) {}
        @Override
        public void setColorFilter(ColorFilter cf) {}
        
    }

}
